#pragma once

#include "mythread.hxx"
#include "mymutex.hxx"
#include <string>
#include <vector>

using namespace std;

class Mutex;
class FileLog;
class Task;
class TaskManager;

class Cordinator : public Thread
{
public:
	////////////////////////////////////////////////////////////////////////////
	///////////////Constructor and Destructor///////////////////////////////////
	Cordinator(TaskManager* taskManager, FileLog* filelog = NULL);

	void Shutdown(void);
	virtual void PreAction(Task* task) = 0;
	virtual void AfterAction(Task* task) = 0;
	virtual bool NextAction(Task* task) = 0;

	bool IsShutdownRequested(void) const
	{
		return isShutdownRequested;
	}

protected:
	void ReleaseTasks(std::vector<Task*>& tasks, Mutex& theLock);
	void PrepareTasks(std::vector<Task*>& tasks, Mutex& theLock);

	void DisplayMsg(string msg, int msgType) const;

	virtual void Execute(Thread::Arg arg) = 0;

	TaskManager* taskManager;
	std::vector<Task*> tasks;
	mutable Mutex tasksInUse;

	volatile long captchaedNum;

	bool isShutdownRequested;
	FileLog* _filelog;
	
};